<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Arrays &mdash; PowerShell fuer Systemintegratoren</title>
    
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="top" title="PowerShell fuer Systemintegratoren" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="Allgemeiner Index"
             accesskey="I">Index</a></li>
        <li><a href="../index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/PowerShell_icon_small.png" alt="Logo"/>
            </a></p>
  <h3>Diese Seite</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/loesung_rst/Loesung_PowerShell_Arrays.txt"
           rel="nofollow">Quelltext anzeigen</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Schnellsuche</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Los" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Geben Sie Suchbegriffe oder einen Modul-, Klassen- oder Funktionsnamen ein.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="arrays">
<h1>Arrays<a class="headerlink" href="#arrays" title="Permalink zu dieser Überschrift">¶</a></h1>
<ul id="loesung-arrays">
<li><p class="first">Schreiben Sie eine Funktion, die als Parameter einen Pfad erwartet und anschließend den Inhalt des Pfades ausgibt</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>listDirectory<span class="o">(</span><span class="nv">$pfad</span><span class="o">)</span>
<span class="o">{</span>

<span class="nb">cd</span> <span class="nv">$pfad</span>
Dir <span class="nv">$pfad</span> |Out-File  C:<span class="se">\t</span>emp<span class="se">\A</span>usgabe.txt

<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Wie überprüfen Sie, ob ein Array einen bestimmten Wert enthält</p>
<blockquote>
<div><p>Mit Hilfe des -contains - Operators</p>
</div></blockquote>
</li>
<li><p class="first">Wie überprüfen Sie, ob zwei Arrays gleich sind</p>
<blockquote>
<div><p>Folgende Funktion überprüft zwei Arrays auf Gleichheit</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="k">function </span>AreArraysEqual<span class="o">(</span><span class="nv">$a1</span>, <span class="nv">$a2</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span> -isnot <span class="o">[</span>array<span class="o">]</span> -or <span class="nv">$a2</span> -isnot <span class="o">[</span>array<span class="o">])</span> <span class="o">{</span>
          throw <span class="s2">&quot;Both inputs must be an array&quot;</span>
        <span class="o">}</span>
        <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span>.Rank -ne <span class="nv">$a2</span>.Rank<span class="o">)</span> <span class="o">{</span>
          <span class="k">return</span> <span class="nv">$false</span>
        <span class="o">}</span>
        <span class="k">if</span> <span class="o">([</span>System.Object<span class="o">]</span>::ReferenceEquals<span class="o">(</span><span class="nv">$a1</span>, <span class="nv">$a2</span><span class="o">))</span> <span class="o">{</span>
          <span class="k">return</span> <span class="nv">$true</span>
        <span class="o">}</span>
        <span class="k">for</span> <span class="o">(</span><span class="nv">$r</span> <span class="o">=</span> 0; <span class="nv">$r</span> -lt <span class="nv">$a1</span>.Rank; <span class="nv">$r</span>++<span class="o">)</span> <span class="o">{</span>
          <span class="k">if</span> <span class="o">(</span><span class="nv">$a1</span>.GetLength<span class="o">(</span><span class="nv">$r</span><span class="o">)</span> -ne <span class="nv">$a2</span>.GetLength<span class="o">(</span><span class="nv">$r</span><span class="o">))</span> <span class="o">{</span>
                        <span class="k">return</span> <span class="nv">$false</span>
          <span class="o">}</span>
        <span class="o">}</span>

        <span class="nv">$enum1</span> <span class="o">=</span> <span class="nv">$a1</span>.GetEnumerator<span class="o">()</span>
        <span class="nv">$enum2</span> <span class="o">=</span> <span class="nv">$a2</span>.GetEnumerator<span class="o">()</span>

        <span class="k">while</span> <span class="o">(</span><span class="nv">$enum1</span>.MoveNext<span class="o">()</span> -and <span class="nv">$enum2</span>.MoveNext<span class="o">())</span> <span class="o">{</span>
          <span class="k">if</span> <span class="o">(</span><span class="nv">$enum1</span>.Current -ne <span class="nv">$enum2</span>.Current<span class="o">)</span> <span class="o">{</span>
                        <span class="k">return</span> <span class="nv">$false</span>
          <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="nv">$true</span>
<span class="o">}</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Ein String ist eigentlich ein Array aus Elementen des Datentyps CHAR.</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$test</span> <span class="o">=</span> <span class="s2">&quot;Hello&quot;</span>
<span class="nv">$test</span><span class="o">[</span>0<span class="o">]</span>.Gettype<span class="o">()</span>

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Char                                     System.ValueType
</pre></div>
</div>
<p>Die Ansprechen eines einzelnen Elementes ist auch mit $test[1] problemlos möglich. Wie können Sie aber ein einzelnes Element eines bestehenden Strings mit Hilfe der Array-Indiziierung ändern, also etwas so</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$integerarray</span> <span class="o">=</span> 1,2,2
<span class="nv">$integerarray</span><span class="o">[</span>2<span class="o">]</span> <span class="o">=</span> 345  <span class="c"># geht</span>

<span class="nv">$test</span> <span class="o">=</span> <span class="s2">&quot;Hello&quot;</span>
<span class="nv">$test</span><span class="o">[</span>0<span class="o">]</span> <span class="o">=</span> <span class="s2">&quot;F&quot;</span>                  <span class="c">#Fehlermeldung</span>
</pre></div>
</div>
<p><strong>Lösung:</strong></p>
<p>Das Umwandeln mit Hilfe der String-Methode ToCharArray() erzeugt einen Array aus CHAR-Elementen, die verändert werden können.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$ergebnis</span> <span class="o">=</span> <span class="nv">$test</span>.ToCharArray<span class="o">()</span>
<span class="nv">$ergebnis</span><span class="o">[</span>0<span class="o">]</span> <span class="o">=</span> <span class="s2">&quot;F&quot;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Überprüfen sie, ob ein bestimmter Wert in einem Array enthalten ist, z.B.</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$arrColors</span> <span class="o">=</span> <span class="s2">&quot;blue&quot;</span>, <span class="s2">&quot;red&quot;</span>, <span class="s2">&quot;green&quot;</span>, <span class="s2">&quot;yellow&quot;</span>, <span class="s2">&quot;white&quot;</span>, <span class="s2">&quot;pink&quot;</span>, <span class="s2">&quot;orange&quot;</span>, <span class="s2">&quot;turquoise&quot;</span>
</pre></div>
</div>
<p>Prüfen Sie, ob der Wert &#8220;green&#8221; enthalten ist</p>
<div class="highlight-sh"><div class="highlight"><pre>foreach<span class="o">(</span><span class="nv">$element</span> in <span class="nv">$arrColors</span><span class="o">)</span>
<span class="o">{</span>
        <span class="k">if</span><span class="o">(</span><span class="nv">$element</span> -eq <span class="s2">&quot;green&quot;</span><span class="o">)</span>
        <span class="o">{</span>
                Write-Host
        <span class="o">}</span>
        <span class="k">else</span>
        <span class="o">{</span>
                Write-Host <span class="s2">&quot;Not found&quot;</span>
        <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<p>Es geht natürlich in Powershell auch einfacher</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$arrColors</span> <span class="o">=</span> <span class="s2">&quot;blue&quot;</span>, <span class="s2">&quot;red&quot;</span>, <span class="s2">&quot;green&quot;</span>, <span class="s2">&quot;yellow&quot;</span>, <span class="s2">&quot;white&quot;</span>, <span class="s2">&quot;pink&quot;</span>, <span class="s2">&quot;orange&quot;</span>, <span class="s2">&quot;turquoise&quot;</span>

<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$arrColors</span> -contains <span class="s2">&quot;green&quot;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Haben wir Elemente, die mit &#8220;bl&#8221; beginnen</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$arrColors</span> <span class="o">=</span> <span class="s2">&quot;blue&quot;</span>, <span class="s2">&quot;red&quot;</span>, <span class="s2">&quot;green&quot;</span>, <span class="s2">&quot;yellow&quot;</span>, <span class="s2">&quot;white&quot;</span>, <span class="s2">&quot;pink&quot;</span>, <span class="s2">&quot;orange&quot;</span>, <span class="s2">&quot;turquoise&quot;</span>, <span class="s2">&quot;black&quot;</span>

<span class="nv">$arrColors</span> -like <span class="s2">&quot;bl*&quot;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Sortieren sie den obigen Array alphabetisch</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$arrColors</span> <span class="o">=</span> <span class="s2">&quot;blue&quot;</span>, <span class="s2">&quot;red&quot;</span>, <span class="s2">&quot;green&quot;</span>, <span class="s2">&quot;yellow&quot;</span>, <span class="s2">&quot;white&quot;</span>, <span class="s2">&quot;pink&quot;</span>, <span class="s2">&quot;orange&quot;</span>, <span class="s2">&quot;turquoise&quot;</span>, <span class="s2">&quot;black&quot;</span>

<span class="nv">$arrColors</span> <span class="o">=</span> <span class="nv">$arrColors</span> | Sort-Object
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Vergleichen Sie die Geschwindigkeit beim Erstellen und Suchen der folgenden Datenstrukturen: Array vs. Hashtable. Welches Ergebnis stellen Sie fest ?</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre><span class="nv">$testarray</span> <span class="o">=</span> @<span class="o">()</span>
<span class="nv">$testhash</span> <span class="o">=</span> @<span class="o">{}</span>

Write-Host <span class="s2">&quot;Array anlegen&quot;</span>
Measure-Command <span class="o">{</span>
        <span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 1; <span class="nv">$i</span> -lt 10000;<span class="nv">$i</span>++<span class="o">)</span>
        <span class="o">{</span>
                <span class="nv">$testarray</span> +<span class="o">=</span> <span class="nv">$i</span>
        <span class="o">}</span>
<span class="o">}</span>

Write-Host <span class="s2">&quot;Hash anlegen&quot;</span>
Measure-Command <span class="o">{</span>
        <span class="k">for</span><span class="o">(</span><span class="nv">$i</span> <span class="o">=</span> 1; <span class="nv">$i</span> -lt 10000;<span class="nv">$i</span>++<span class="o">)</span>
        <span class="o">{</span>
                <span class="nv">$testhash</span>.Add<span class="o">(</span><span class="nv">$i</span>, <span class="nv">$i</span><span class="o">)</span>
        <span class="o">}</span>
<span class="o">}</span>

Write-Host <span class="s2">&quot;Array enthält 9000&quot;</span>
Measure-Command<span class="o">{</span>
        <span class="nv">$testarray</span> -contains 9000
<span class="o">}</span>

Write-Host <span class="s2">&quot;Hash enthält 9000&quot;</span>
Measure-Command<span class="o">{</span>
        <span class="nv">$testhash</span>.get_Item<span class="o">(</span>9000<span class="o">)</span>
<span class="o">}</span>
</pre></div>
</div>
<p>Das Skript erzeugt folgende Ergebnisse auf einem ASUS Netbook (N270, 2 GByte Ram, Windows 7):</p>
<div class="highlight-sh"><div class="highlight"><pre>Array anlegen
...
TotalSeconds      : 27,2567811
TotalMilliseconds : 27256,7811

Hash anlegen
TotalSeconds      : 0,3869414
TotalMilliseconds : 386,9414

Array enthält 9000
.....
TotalSeconds      : 0,0359344
TotalMilliseconds : 35,9344

Hash enthält 9000
...
TotalSeconds      : 0,0001328
TotalMilliseconds : 0,1328
</pre></div>
</div>
<p><strong>Fazit:</strong></p>
<p>Gerade wenn Listen zur Laufzeit wachsen ist ein Array schon bei kleinen Datenmengen relativ langsam. Auch die Suche innerhalb von kleinen Datenmengen wird über eine Hashtable deutlich beschleunigt.</p>
</div></blockquote>
</li>
<li><p class="first">Sie besitzen eine Textdatei mit Familienname und Loginname der Firmenmitarbeiter. Sie umfasst über 9000 Einträge und soll dazu hergenommen werden, um &#8220;vergessene&#8221; Loginnamen zu suchen. Überlegen Sie sich, wie Sie diese Textdatei innerhalb von Powershell-Skripten nutzen können.</p>
</li>
</ul>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$fam_login</span> <span class="o">=</span> @<span class="o">{}</span>

Import-Csv user_passwd.csv | ForEach-Object <span class="o">{</span> <span class="nv">$fam_login</span><span class="o">[</span><span class="nv">$_</span>.username<span class="o">]</span> <span class="o">=</span> <span class="nv">$_</span>.passwort<span class="o">}</span>
<span class="nv">$fam_login</span>
<span class="nv">$fam_login</span><span class="o">[</span><span class="s2">&quot;?9d8k6L2&quot;</span><span class="o">]</span>

<span class="nv">$fam_login</span>.GetEnumerator<span class="o">()</span> | <span class="k">select </span>key, value | <span class="nb">export</span>-csv .<span class="se">\t</span>estexport.csv

<span class="nv">$fam_login</span> | <span class="nb">export</span>-clixml -path fam_login.xml


<span class="nv">$fam2</span> <span class="o">=</span> import-clixml -path fam_login.xml
</pre></div>
</div>
<p>Durch die Nutzung der export-clixml und import-clixml - Commandlets werden Dateien im XML-Format geschrieben. Diese machen es einfacher möglich, Metainformationen zu lesen und zu schreiben.</p>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="Allgemeiner Index"
             >Index</a></li>
        <li><a href="../index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, STE.
      Mit <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1 erstellt.
    </div>
  </body>
</html>